using Digitalmes.Application.Mes.Equipments.Machinery;
using Digitalmes.Application.Mes.Equipments.Machinery.Commands;
using Digitalmes.Application.Mes.Equipments.Machinery.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Equipments;

/// <summary>
/// 设备终结点。
/// </summary>
public sealed class MachineryEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/equipment", "machinery")
            .RequireAuthorization()
            .MapGet(GetMachineryListAsync, "list")
            .MapGet(GetMachineryPagedListAsync, "list-page")
            .MapPost(CreateMachineryAsync)
            .MapPut(UpdateMachineryAsync, "{id}")
            .MapDelete(DeleteMachineryAsync, "{id}");
    }

    [EndpointDescription("查询设备，包含产线和工位信息")]
    public async Task<IApiResult> GetMachineryListAsync(ISender sender, [AsParameters] GetMachineryListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询设备")]
    public async Task<IApiResult> GetMachineryPagedListAsync(ISender sender, [AsParameters] GetMachineryListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建设备")]
    public async Task<IApiResult> CreateMachineryAsync(ISender sender, EquipMachineryDto dto)
    {
        var result = await sender.Send(new CreateMachineryCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新设备")]
    public async Task<IApiResult> UpdateMachineryAsync(ISender sender, long id, EquipMachineryDto dto)
    {
        var result = await sender.Send(new UpdateMachineryCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除设备")]
    public async Task<IApiResult> DeleteMachineryAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteMachineryCommand(id));
        return result.ToApiResult();
    }
}
